const axios = require('axios')
axios.defaults.withCredentials = true

module.exports = options => {
  return async(req, res) => {
    const { url, headers, body, method } = req
    let mockConfig = {
      backendUrl: 'http://47.98.61.156:9009',
    }
    try {
      const result = await axios({
        method,
        url: mockConfig.backendUrl + url,
        data: body,
        headers: {
          'X-Requested-With': 'XMLHttpRequest',
          'token': headers['token'] || '',
          cookie: headers.cookie || ''
        }
      })
      if (result.headers['set-cookie']) {
        res.setHeader('set-cookie', result.headers['set-cookie'])
      }
      res.send(result.data)
    } catch (err) {
      console.log(err)
      res.send({
        code: 100,
        message: 'mock server 转发请求错误',
        from: 'mock server'
      })
    }
  }
}
